package com.dyna.cp.mybatis.controller;

import com.dyna.cp.mybatis.domain.entity.DemoEduExperience;
import com.dyna.cp.mybatis.domain.entity.DemoUser;
import com.dyna.cp.mybatis.domain.resp.DemoFriendListResp;
import com.dyna.cp.mybatis.service.MybatisService;
import com.dyna.domain.resp.PageResult;
import com.dyna.domain.resp.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("demo模块/cp/mybatis")
@RestController
@RequestMapping("/mybatis")
public class MybatisController {

    private final MybatisService mybatisService;

    @Autowired
    public MybatisController(MybatisService mybatisService) {
        this.mybatisService = mybatisService;
    }

    @ApiOperation("添加用户数据")
    @RequestMapping("/addUser")
    public R<Boolean> addUser(@RequestBody DemoUser demoUser) {
        return R.data(mybatisService.addUser(demoUser));
    }

    @ApiOperation("批量添加用户数据")
    @RequestMapping("/batchAddUser")
    public R<Boolean> batchAddUser(@RequestBody DemoUser demoUser) {
        return R.data(mybatisService.batchAddUser(demoUser));
    }

    @ApiOperation("查询用户详情")
    @GetMapping("/detail/{id}")
    public R<DemoUser> getDetail(@PathVariable("id") Integer id) {
        return R.data(mybatisService.getDetail(id));
    }

    @ApiOperation("根据用户名称查询")
    @GetMapping("/getUserByName")
    public R<List<DemoUser>> getUserByName(@RequestParam("name") String name) {
        return R.data(mybatisService.getUserByName(name));
    }

    @ApiOperation("删除用户")
    @GetMapping("/del/{id}")
    public R<Boolean> del(@PathVariable("id") Integer id) {
        return R.isSuccess(mybatisService.del(id));
    }

    @ApiOperation("手动事务")
    @GetMapping("/manualTransactional")
    public R<Boolean> manualTransactional() {
        return R.isSuccess(mybatisService.manualTransactional());
    }

    @ApiOperation("添加教育经历")
    @PostMapping("/addEduExperience")
    public R<Boolean> addEduExperience(@RequestBody DemoEduExperience body) {
        return R.isSuccess(mybatisService.addEduExperience(body));
    }

    @ApiOperation("测试mybatis连表查询集合字段")
    @GetMapping("/testMybatisJoinFriend/{id}")
    public R<DemoFriendListResp> testMybatisJoinFriend(@PathVariable("id") Integer id) {
        return R.data(mybatisService.testMybatisJoinFriend(id));
    }

    @ApiOperation("测试mybatis连表分页查询集合字段")
    @GetMapping("/testMybatisJoinFriendPage")
    public R<PageResult<DemoFriendListResp>> testMybatisJoinFriendPage() {
        return R.data(mybatisService.testMybatisJoinFriendPage());
    }

}